<?php

require_once 'assignmentManager.php';

class Course
{
    private $userID;
    private $courseName;
    private $courseID;
    private $startDate;
    private $endDate;
    private $currentAverage;
    private $assignment;
    private $building;
    private $room;
    private $professorID;

    public function setUserID($newUser) { $this->userID = $newUser; }
    public function setName($newName) { $this->courseName = $newName; }
    public function setID($newID) { $this->courseID = $newID; }
    public function setStartDate($newStart) { $this->startDate = $newStart; }
    public function setEndDate($newEnd) { $this->endDate = $newEnd; }
    public function setCurrentAvg($newAvg) { $this->currentAverage = $newAvg; }
    public function setAssignments($newAssignments) { $this->assignment = $newAssignments; }
    public function setBuilding($new) { $this->building = $new; }
    public function setRoom($new) { $this->room = $new; }
    public function setProfessorID($newProfessor) { $this->professorID = $newProfessor; }

    public function getUserID() { return $this->userID; }
    public function getName() { return $this->courseName; }
    public function getID() { return $this->courseID; }
    public function getStartDate() { return $this->startDate; }
    public function getEndDate() { return $this->endDate; }
    public function getCurrentAvg() { return $this->currentAverage; }
    public function getAssignments() { return $this->assignment; }
    public function getBuilding() { return $this->building; }
    public function getRoom() { return $this->room; }
    public function getProfessorID() { return $this->professorID; }

    public function calculateAverage()
    {
        $assignmentArray = array();
        $assignmentArray = assignmentManager::getCourseAssignments($this);
        $newArray = array();
        $newArray = $assignmentArray;
        $earnedPoints = array();
        $potentPoints = array();
        $i = 0;
        while ($i < sizeof($newArray))
        {
            
            $current = array_shift($newArray);
            $weightID = $current->getWeight();
            
            $earnedPoints[$weightID] = $earnedPoints[$weightID] + $current->getEarnedPoints();
            $potentPoints[$weightID] = $potentPoints[$weightID] + $current->getPotentialPoints();

            $j = 0;
            $arrayCopy = $newArray;
            while ($j < sizeof($newArray))
            {
                
                $check = array_shift($newArray);
                if ($check->getWeight() == $weightID)
                {
                    $earnedPoints[$weightID] = $earnedPoints[$weightID] + $check->getEarnedPoints();
                    $potentPoints[$weightID] = $potentPoints[$weightID] + $check->getPotentialPoints();
                } else {
                    array_push($newArray, $check);
                }
                $j++;
                
            }
            $i++;
        }


        $grade = 0;
        foreach($earnedPoints as $ID => $earned)
        {
            if ($ID == 0)
            {
                $percent = 100;
            } else {
                $percent = assignmentManager::getWeight($ID);
            }
            $potent = $potentPoints[$ID];
            //hey I'm just holding this here so that I can get the
            //page set up right until it doesn't throw a divide by zero
            //if $potent == 0.0
            if($potent == 0.0)
            {
                echo("<p>divide by zero</p>");
                $grade = -1;
            }
            else
            {
                $grade = ($earned/$potent)*($percent) + $grade;
            }
        }

        $grade = round($grade, 0);
        return $grade;
    }
}
?>
